<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>-->
    <!--<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>-->
    <!--<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>-->
    <!--[endif]-->
    <script type="text/javascript" src="../../common/js/public.js"></script>
    <script type="text/javascript" src="../../common/js/publicUtils.js"></script>
<style>
    .layui-table-cell{
        height:34px !important;
        line-height: 34px !important;
    }
    .layui-unselect dl { max-height:150px; }
</style>
</head>
<body>
<div class="layui-fluid" id="flowDiv">
    <div class="zTreeBackground right" style="padding-left:1%;width:98%">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">字段信息</li>
                <li>基本信息</li>
                <li>生成信息</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <table class="layui-hide" id="datatable" lay-filter="datatable"></table>
                    <div style="border-top:1px solid red;padding-left: 40%;padding-top: 10px;">
                            <button type="button" style="float: left;margin-left: 12px" class="layui-btn layui-btn-normal" onclick="updateTableColumn()">确定</button>
                            <button type="button" class="layui-btn layui-btn-primary" onclick="closeIndex()">关闭</button>
                    </div>
                </div>
                <div class="layui-tab-item">
                        <div class="layui-row">
                            <form class="layui-form"  id="dataformsb" lay-filter="dataformsb">
                                <input type="hidden" name="tableId">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" for="tableName"><span style="color: red;">*</span>表名称</label>
                                        <div class="layui-input-inline" style="width:400px;">
                                            <input name="tableName" id="tableName" lay-verify="required" autocomplete="off" class="layui-input" type="text" readonly>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label" for="tableComment"><span style="color: red;">*</span>表描述</label>
                                        <div class="layui-input-inline" style="width:400px;">
                                            <input name="tableComment" id="tableComment" lay-verify="required" autocomplete="off" class="layui-input" type="text">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label" for="className"><span style="color: red;">*</span>实体类名称</label>
                                        <div class="layui-input-inline" style="width:400px;">
                                            <input name="className" id="className" lay-verify="required" autocomplete="off" class="layui-input" type="text">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label" for="functionAuthor"><span style="color: red;">*</span>作&emsp;者</label>
                                        <div class="layui-input-inline" style="width:400px;">
                                            <input name="functionAuthor" id="functionAuthor" lay-verify="required" autocomplete="off" class="layui-input" type="text">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item layui-form-text">
                                    <label class="layui-form-label" for="remark">备&emsp;注</label>
                                    <div class="layui-input-block" style="width:920px;">
                                        <textarea placeholder="请输入内容" id="remark" class="layui-textarea" name="remark"></textarea>
                                    </div>
                                </div>
                            </form>
                        </div>
                    <div style="border-top:1px solid red;padding-left: 40%;padding-top: 10px; ">
                        <button type="button" style="float: left;margin-left: 12px" class="layui-btn layui-btn-normal" onclick="updateTable(1)">确定</button>
                        <button type="button" class="layui-btn layui-btn-primary" onclick="closeIndex()">关闭</button>
                    </div>

                </div>
                <div class="layui-tab-item">
                    <div class="layui-row">
                        <form class="layui-form"  id="dataformnb" lay-filter="dataformnb">
                            <input type="hidden" name="tableId">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label"><span style="color: red;">*</span>生&nbsp;成&nbsp;模&nbsp;板</label>
                                    <div class="layui-input-inline" style="width:400px;">
                                        <select id="tplCategory" name='tplCategory' lay-filter='tplCategory' style="width: 100%">
                                            <option value="crud" >单表（增删改查）</option>
                                            <option value="tree" >树表（增删改查）</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label" for="packageName"><span style="color: red;">*</span>生成包路径</label>
                                    <div class="layui-input-inline" style="width:400px;">
                                        <input name="packageName" id="packageName" lay-verify="required" autocomplete="off" class="layui-input" type="text">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label" for="moduleName"><span style="color: red;">*</span>生成模块名</label>
                                    <div class="layui-input-inline" style="width:400px;">
                                        <input name="moduleName" id="moduleName" lay-verify="required" autocomplete="off" class="layui-input" type="text">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label" for="businessName"><span style="color: red;">*</span>生成业务名</label>
                                    <div class="layui-input-inline" style="width:400px;">
                                        <input name="businessName" id="businessName" lay-verify="required" autocomplete="off" class="layui-input" type="text">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label" for="functionName"><span style="color: red;">*</span>生成功能名</label>
                                    <div class="layui-input-inline" style="width:400px;">
                                        <input name="functionName" id="functionName" lay-verify="required" autocomplete="off" class="layui-input" type="text">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-row otherparam" style="display: none">
                                <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                                    <legend>其他信息</legend>
                                </fieldset>
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label"><span style="color: red;">*</span>树编码字段</label>
                                        <div class="layui-input-inline" style="width:400px;">
                                            <select id="treeCode" name='treeCode' style="width: 100%">
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label"><span style="color: red;">*</span>树父编码字段</label>
                                        <div class="layui-input-inline" style="width:400px;">
                                            <select id="treeParentCode" name='treeParentCode' style="width: 100%">
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label"><span style="color: red;">*</span>树名称字段</label>
                                        <div class="layui-input-inline" style="width:400px;">
                                            <select id="treeName" name='treeName' style="width: 100%">
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>


                    <div style="border-top:1px solid red;padding-left: 40%;padding-top: 10px;">
                        <button type="button" style="float: left;margin-left: 12px" class="layui-btn layui-btn-normal" onclick="updateTable(2)">确定</button>
                        <button type="button" class="layui-btn layui-btn-primary" onclick="closeIndex()">关闭</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<SCRIPT type="text/javascript">
    var tableData,form,tableObj,table,tableId,tplCategory,treedata;
    var javaTypeData = ["String","Integer","Long","Double","BigDecimal","Date"];
    var htmlTypeData = ["input","textarea","select","radio","checkbox","datetime"];
    var queryTypeData = ["=","!=",">",">=","<","<=","LIKE","BETWEEN"];
    var queryTypeJson = ["EQ","NE","GT","GTE","LT","LTE","LIKE","BETWEEN"];
    $(function(){
        //loadbutton("toolbar");//加载按钮loadbutton(id)，id为button容器id，不传默认为buttongroup
        //loadTable();
        layui.use('form',function(){
            var form = layui.form;
            form.on('select(tplCategory)', function(data){
                var flag=data.value;
                if(flag=="crud"){
                    $(".otherparam").css("display","none");
                }else{
                    $(".otherparam").css("display","");
                }
            });
        })
    })

    function loadQueryTypeData(type,remark,data,columnId){
        var option = "<select name='"+type+"' lay-filter='selectDemo'  dataId='"+columnId+"'>";
        $.each(data,function (i,e) {
            if(queryTypeJson[i]==remark){
                option += "<option style='text-align: left' value='"+queryTypeJson[i]+"' SELECTED>"+e+"</option>";
            }else{
                option += "<option style='text-align: left' value='"+queryTypeJson[i]+"'>"+e+"</option>";
            }
        })
        option += "</select>";
        return option;
    }

    function loadSelect(type,remark,data,columnId){
        var option = "<select name='"+type+"' lay-filter='selectDemo'  dataId='"+columnId+"'>";
        $.each(data,function (i,e) {
            if(e==remark){
                option += "<option style='text-align: left' value='"+e+"' SELECTED>"+e+"</option>";
            }else{
                option += "<option style='text-align: left' value='"+e+"'>"+e+"</option>";
            }
        })
        option += "</select>";
        return option;
    }

    function loadcheck(type,val,columnId){
        var html="";
        if(val==1){
            html="<input name='"+type+"' type='checkbox' dataId='"+columnId+"' lay-filter='checkboxDemo' checked  lay-skin='primary' />";
        }else{
            html="<input name='"+type+"' type='checkbox' dataId='"+columnId+"' lay-filter='checkboxDemo'  lay-skin='primary' />";
        }
        return html;
    }

    /* *
     * @Author bxf
     * @Description 初始化table
     * @Date 17:51 2019/8/30
     * @param null
     * @return
     */
    function initcolumns(val){
        tableId = val;
        var tableurl = publicUrl+'tool/gen/column/list';
        layui.use(['form','table','element'], function(){
            var element = layui.element;
            form = layui.form;
            table = layui.table;
            tableObj = table.render({
                elem: '#datatable',
                size: 'sm',
                even: true, //开启隔行背景
                height: 'full-180',
                method: 'post',
                headers: {"Authorization":getData("Authorization")},
                url: tableurl, //stationId='+window.parent.window.parentId,//8AE7DECA-8E41-4FAB-8B30-9FD1B0B7FA33,78A9E5D8-20FB-4436-BBE8-A25EAC38D056
                where:{
                    "tableId":val,
                },
                response: {
                    statusCode: 2000,
                },
                parseData: function(res){ //res 即为原始返回的数据
                    treedata = res.data.list;
                    return formatTableParseData(res);
                },
                request: {
                    pageName: 'pageIndex' //页码的参数名称，默认：page
                    ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
                },
                cols: [[

                    {
                        type: 'numbers',
                        title: '序号',
                        width: 60,
                        align: 'center',
                    },{
                        field: 'columnName',
                        title: '字段名称',
                        width: "10%",
                        align: 'center'
                    }, {
                        field: 'columnComment',
                        title: 'Table列名',
                        width: "10%",
                        align: 'center',
                        templet:function(r){
                            var columnComment = (r.columnComment != null && r.columnComment != 'null' && r.columnComment != '')?r.columnComment:'';
                            return "<input type='text' name='columnComment' data-index='"+r.columnId+"' value='"+columnComment+"' placeholder='请输入列描述' class='layui-input inputDemo'>";
                        }
                    },{
                        field: 'columnType',
                        title: '类型',
                        width: "6%",
                        align: 'center'
                    },{
                        field: 'javaField',
                        title: '实体字段',
                        width: "8%",
                        align: 'center'
                    },{
                        field: 'javaType',
                        title: 'Java类型',
                        width: "8%",
                        align: 'center',
                        templet:function(r){
                            var option = loadSelect("javaType",r.javaType,javaTypeData,r.columnId);
                            return option;
                        }
                    },{
                        field: 'isInsert',
                        title: '新增',
                        width: "4%",
                        align: 'center',
                        templet:function(r){
                            return loadcheck('isInsert',r.isInsert,r.columnId);
                        }
                    },{
                        field: 'isEdit',
                        title: '编辑',
                        width: "4%",
                        align: 'center',
                        templet:function(r){
                            return loadcheck('isEdit',r.isEdit,r.columnId);
                        }
                    },{
                        field: 'isList',
                        title: '显示',
                        width: "4%",
                        align: 'center',
                        templet:function(r){
                            return loadcheck('isList',r.isList,r.columnId);
                        }
                    },{
                        field: 'isQuery',
                        title: '查询',
                        width: "4%",
                        align: 'center',
                        templet:function(r){
                            return loadcheck('isQuery',r.isQuery,r.columnId);
                        }
                    },{
                        field: 'isMainQuery',
                        title: '主查询',
                        width: "4%",
                        align: 'center',
                        templet:function(r){
                            return loadcheck('isMainQuery',r.isMainQuery,r.columnId);
                        }
                    },{
                        field: 'isRequired',
                        title: '必填',
                        width: "4%",
                        align: 'center',
                        templet:function(r){
                            return loadcheck('isRequired',r.isRequired,r.columnId);
                        }
                    },{
                        field: 'isExcel',
                        title: 'excel导入',
                        width: "5%",
                        align: 'center',
                        templet:function(r){
                            return loadcheck('isExcel',r.isExcel,r.columnId);
                        }
                    },{
                        field: 'excelName',
                        title: 'excel列名',
                        width: "10%",
                        align: 'center',
                        templet:function(r){
                            var excelName = (r.excelName != null && r.excelName != 'null' && r.excelName != '')?r.excelName:'';
                            return "<input type='text' name='excelName' data-index='"+r.columnId+"' value='"+excelName+"' placeholder='请输入excel名称' class='layui-input inputDemo'>";
                        }
                    },{
                        field: 'queryType',
                        title: '查询方式',
                        width: "10%",
                        align: 'center',
                        templet:function(r){
                            var option = loadQueryTypeData("queryType",r.queryType,queryTypeData,r.columnId);
                            return option;
                        }
                    },{
                        field: 'htmlType',
                        title: '显示类型',
                        width: "10%",
                        align: 'center',
                        templet:function(r){
                            var option = loadSelect("htmlType",r.htmlType,htmlTypeData,r.columnId);
                            return option;
                        }
                    }]],
                page: false,
                done: function(res, curr, count){
                    //数据渲染完的回调。
                    //由于layui 设置了超出隐藏，所以这里改变下，以兼容操作按钮的下拉菜单
                    $(".layui-table-box, .layui-table-cell").css('overflow', 'visible');
                    tableData = table.cache["datatable"];
                    //console.info(tableData);
                    //监听多选框
                    $(".inputDemo").on("blur",function(){
                        var index = $(this).parents("tr:eq(0)").attr('data-index');
                        var name = $(this).attr('name');
                        tableData[index][name] = $(this).val();
                    })
                    var option = "";
                    layui.use('form',function(){
                        var form = layui.form;
                        $.each(treedata,function(i,e){
                            var comment = e.columnComment?e.columnComment:'';
                            option += "<option value="+e.columnName+">"+e.columnName+" "+comment+"</option>";
                        })
                        $("#treeCode,#treeParentCode,#treeName").html(option);
                        form.render();
                    })
                }
            });
            loadtableData();
            element.render();
            //监听单元格编辑
            table.on('edit(datatable)', function(obj){
                var value = obj.value //得到修改后的值
                    ,data = obj.data //得到所在行所有键值
                    ,field = obj.field; //得到字段
                layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
            });
            //监听地址选择操作
            form.on('select(selectDemo)', function (obj) {
                var index = $(obj.elem).parents("tr:eq(0)").attr('data-index');
                var name = obj.elem.getAttribute('name');
                tableData[index][name] = obj.value;
                console.info(tableData);
            })
            //监听多选框
            form.on('checkbox(checkboxDemo)', function (obj) {
                var index = $(obj.elem).parents("tr:eq(0)").attr('data-index');
                var name = obj.elem.getAttribute('name');
                if(name=="isQuery"){
                    var obj_new = $(obj.elem).parents("tr:eq(0)").find("input[name=isMainQuery]");
                    if($(obj.elem).attr("checked")){
                        if(obj_new.attr("checked")){
                            obj_new.attr("checked", false); //注意这里使用的是attr()
                            layui.form.render(); //重新渲染显示效果
                            var index_new = obj_new.parents("tr:eq(0)").attr('data-index');
                            var name_new = obj_new.attr('name');
                            tableData[index_new][name_new] = '0';
                        }
                    }
                }
                if(name=="isExcel"){
                    var obj_new = $(obj.elem).parents("tr:eq(0)").find("input[name=excelName]");
                    if($(obj.elem).attr("checked")){
                        if(!obj_new.val()){
                           layer.msg("请输入该字段对应的excel名称");
                        }
                    }
                }
                if(name=="isMainQuery"){
                    var obj_new = $(obj.elem).parents("tr:eq(0)").find("input[name=isQuery]");
                    if($(obj.elem).attr("checked")){
                        if(obj_new.attr("checked")){
                            obj_new.attr("checked", false); //注意这里使用的是attr()
                            form.render(); //重新渲染显示效果
                            var index_new = obj_new.parents("tr:eq(0)").attr('data-index');
                            var name_new = obj_new.attr('name');
                            tableData[index_new][name_new] = '0';
                        }
                    }
                }
                tableData[index][name] = $(obj.elem).attr("checked")=='checked'?'1':'0';
                //console.info(tableData);
            })

        });
    }
    function loadtableData() {
        layui.use(['form', 'laydate','layer'], function() {
            var form = layui.form,laydate = layui.date,layer = layui.layer;
            publicAjax(null,'tool/gen/editTableBytableId',{'tableId':tableId},null,null,function(r){
                if(r.code==2000){
                    var tableData = r.data;
                    form.val("dataformsb",{
                        "tableId":tableData.tableId,
                        "tableName":tableData.tableName,
                        "tableComment":tableData.tableComment,
                        "className":tableData.className,
                        "functionAuthor":tableData.functionAuthor,
                        "remark":tableData.remark
                    });
                    if(tableData.tplCategory=="tree"){
                        $(".otherparam").css("display","");
                    }
                    var data = tableData.options?eval('('+tableData.options+')'):{};
                    form.val("dataformnb",{
                        "tableId":tableData.tableId,
                        "tplCategory":tableData.tplCategory,
                        "packageName":tableData.packageName,
                        "moduleName":tableData.moduleName,
                        "businessName":tableData.businessName,
                        "functionName":tableData.functionName,
                        "treeCode":data.treeCode,
                        "treeParentCode":data.treeParentCode,
                        "treeName":data.treeName
                    });
                }else{
                    layerror("获取失败",null);
                }
            })
        });

    }

    function updateTableColumn(){
        var data = {};
        data["columns"] = tableData;
        publicAjaxMap(null,'tool/gen/edit',data,null,null,function(r){
            if(r.code==2000){
                laysuccess("编辑成功",null);
            }else{
                layerror("编辑失败",null);
            }
        })
    }

    function updateTable(flag){
        var obj = $(validform(flag).form());
        if(!obj[0]){
            return false;
        }
        var newid = flag==1?"#dataformsb":"#dataformnb";
        var data = $(newid).serializeJson();
        publicAjaxMap(null,'tool/gen/edit',data,null,null,function(r){
            if(r.code==2000){
                laysuccess("编辑成功",null);
            }else{
                layerror("编辑失败",null);
            }
        })
    }

    function closeIndex(){
        parent.moresearch();
        parent.layer.closeAll();
    }

    //编写表单验证函数validform，在验证按钮注册按钮点击事件内调用验证函数对象
    function validform(flag){
        if(flag==1){
            return $("#dataformsb").validate({
                rules : {
                    tableName : {
                        required:true,
                        normalizer: function( value ) {
                            return $.trim( value );
                        }
                    },
                    tableComment : {
                        required:true,
                        normalizer: function( value ) {
                            return $.trim( value );
                        }
                    },
                    className : {
                        required:true,
                        normalizer: function( value ) {
                            return $.trim( value );
                        }
                    },
                    functionAuthor: {
                        required:true,
                        normalizer: function( value ) {
                            return $.trim( value );
                        }
                    },
                    remark: {
                        maxlength: 500,
                        normalizer: function( value ) {
                            return $.trim( value );
                        }
                    }
                },
                messages : {
                    tableName : {
                        required: "表名称不能为空"
                    },
                    tableComment:{
                        required: "表描述不能为空"
                    },
                    className:{
                        required: "实体类名称不能为空"
                    },
                    functionAuthor:{
                        required: "作者不能为空"
                    },
                    remark : {
                        maxlength: "备注长度不可超过500"
                    }
                }
            });
        }else{
            return $("#dataformnb").validate({
                rules : {
                    packageName : {
                        required:true,
                        normalizer: function( value ) {
                            return $.trim( value );
                        }
                    },
                    moduleName : {
                        required:true,
                        normalizer: function( value ) {
                            return $.trim( value );
                        }
                    },
                    businessName : {
                        required:true,
                        normalizer: function( value ) {
                            return $.trim( value );
                        }
                    },
                    functionName: {
                        required:true,
                        normalizer: function( value ) {
                            return $.trim( value );
                        }
                    }
                },
                messages : {
                    packageName : {
                        required: "生成包路径不能为空"
                    },
                    moduleName:{
                        required: "生成模块名不能为空"
                    },
                    businessName:{
                        required: "生成业务名不能为空"
                    },
                    functionName:{
                        required: "生成功能名不能为空"
                    }
                }
            });
        }

    }

</script>
</html>